package com.example.controller;

import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
//import com.example.annotation.Log;
import com.example.common.result.Result;
import com.example.entity.dto.User.DscmaListDto;
import com.example.entity.dto.User.UpdateDeptDto;
import com.example.entity.dto.User.UpdatePwdDto;
import com.example.entity.dto.User.UserLoginDto;
import com.example.entity.vo.User.UserLoginVo;
import com.example.entity.yifei.Cmsme;
import com.example.entity.yifei.Dscma;
import com.example.entity.ymlConfig.YmlConfig;
import com.example.mapper.CmsmeMapper;
import com.example.mapper.DscmaMapper;
import com.example.mapper.UserMapper;
import com.example.service.DscmaService;
import com.example.service.UserService;
import com.example.untils.JwtUtils;
import com.example.untils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/user")
@Api(tags = "用户相关接口")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private DscmaService dscmaService;

    @Autowired
    private DscmaMapper dscmaMapper;

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private CmsmeMapper cmsmeMapper;

    @ApiOperation("用户列表")
    @GetMapping("userList")
    public Result<List<Dscma>> userList(String name) {
        return userService.queryList(name);
    }

    @ApiOperation("用户列表dscma")
    @PostMapping("dscmaList")
    public Result<List<Dscma>> dscmaList(@RequestBody DscmaListDto dto) {
        List<Cmsme> cmsmeList = cmsmeMapper.selectList(null);
        Map<String, String> cmsmeMap = new HashMap<>();
        for(Cmsme t : cmsmeList) cmsmeMap.put(t.getMe001().trim(), t.getMe002().trim());
        List<Dscma> list = userService.dscmaList(dto);
        for(Dscma t : list) {
            if(StringUtils.isNotEmpty(t.getUdf01())) t.setDeptName(cmsmeMap.get(t.getUdf01().trim()));
        }
        return Result.success(list);
    }

//    @Log("用户登录")
//    @ApiOperation("用户登录")
//    @PostMapping("userLogin")
//    public Result<UserLoginVo> userLogin(@RequestBody UserLoginDto userLoginDto,
//                                         HttpServletRequest request) {
//        return userService.userLogin(userLoginDto, request);
//    }

    @ApiOperation("用户登录")
    @PostMapping("/userLogin")
    public Result userLogin(@RequestBody UserLoginDto userLoginDto) {
        return userService.selectlogin(userLoginDto);
    }

    // 根据token获取用户信息
    @ApiOperation("获取当前登录用户（没有登录，返回401）")
    @PostMapping("getLoginUser")
    public Result getMemberInfo(HttpServletRequest request) {
        String MA001 = JwtUtils.getMA001ByJwtToken(request);
        if(StringUtils.isEmpty(MA001)) {
            return Result.error("用户没有登录", "401");
        }
        return Result.success(dscmaService.getUserInfo(MA001));
    }

    @ApiOperation("修改密码")
    @DSTransactional
    @PostMapping("updatePwd")
    public Result updatePwd(@RequestBody UpdatePwdDto dto) {
        if(StringUtils.isEmpty(dto.getUserId()) || StringUtils.isEmpty(dto.getPassword())
                || StringUtils.isEmpty(dto.getNewPassword())) {
            return Result.error("参数不能为空");
        }
        Integer integer = userMapper.userLogin(new UserLoginDto(dto.getUserId(), dto.getPassword(), YmlConfig.getYFCOMPANY()));
        if(integer == null || integer == 0) {
            return Result.error("用户名或者密码错误", "500");
        }
        userMapper.updatePwd(dto.getUserId(), dto.getNewPassword());
        return Result.success("修改成功");
    }

    @ApiOperation("修改部门")
    @DSTransactional
    @PostMapping("updateDept")
    public Result updateDept(@RequestBody UpdateDeptDto dto) {
        if(StringUtils.isEmpty(dto.getMa001())) {
            return Result.error("用户编码不能为空");
        }
        Dscma dscma = new Dscma();
        dscma.setUdf01(dto.getDeptNo());
        dscmaMapper.update(dscma, new LambdaQueryWrapper<Dscma>().eq(Dscma::getMa001, dto.getMa001()));
        return Result.success("修改成功");
    }

//    @ApiOperation("用户注销")
//    @PostMapping("userLogout")
//    public Result userLogout(HttpServletRequest request) {
//        boolean b = userService.userLogout(request);
//        if(!b) {
//            return Result.error("用户未登录", "401");
//        }
//        return Result.success(b);
//    }

}
